import type { MapMouseEvent } from 'mapbox-gl'
import { BaseInteraction } from './base-interaction'

export abstract class IgnoreMouseDrag extends BaseInteraction {
  private cx = 0
  private cy = 0

  abstract onMouseClick(evt: MapMouseEvent): void

  public onMouseDown(evt: MapMouseEvent): void {
    const oe = evt.originalEvent
    this.cx = oe.clientX
    this.cy = oe.clientY
  }

  public onMouseUp(evt: MapMouseEvent): void {
    const me = evt.originalEvent
    const dx = this.cx - me.clientX
    const dy = this.cy - me.clientY
    const eps = 10
    const drag = dx < -eps || dx > eps || dy < -eps || dy > eps
    if (drag) {
      return
    }
    this.onMouseClick(evt)
  }
}
